﻿package  {
	import flash.display.*;
	import flash.geom.Point;
	public class Unit extends MovieClip{

		var location:Point;
		var target:Point;
		var speed:Number;
		var canvas:MovieClip;
		var map:Map;
		public function Unit(map:Map,canvas:MovieClip,location:Point) {
			this.map=map;
			this.canvas=canvas;
			setLocation(location);
			setTarget(location);
			speed=1;
		}
		public function setLocation(location:Point){
			this.location=location;
			var m2s:Point=map.mapToScreen(location);
			this.x=m2s.x;
			this.y=m2s.y;
		}
		public function setTarget(target:Point){
			this.target=target;
		}
		public function move(){
			var dir:Point=target.subtract(location);
			dir.normalize(1);
			dir.x*=speed;
			dir.y*=speed;
			setLocation(location.add(dir));
		}
		public function update(){
			rotate();
			move();
		}
		public function rotate(){
			var dir:Point=target.subtract(location);
			rotation=Math.atan2(dir.y,dir.x)*180/Math.PI;
		}

	}
	
}
